﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// Scroll View
///  - Grid
///    - 0
///    - 1
///    - 2
///    - 3
///    - 4 
/// </summary>
public class mEndLessCoverFlow : MonoBehaviour
{

    #region outlet-setting
    public Transform mTrans;
    public UIPanel mPanel;
    public UIScrollView mScroll;
    public List<mEndLessEntry> mChildren = new List<mEndLessEntry>();
    #endregion

    /// <summary>
    /// Width or height of the child items for positioning purposes.
    /// </summary>

    public int itemSize = 100;

    protected virtual void Start()
    { 
        /////////////////////////////////////
        mTrans = transform;
        mPanel = NGUITools.FindInParents<UIPanel>(gameObject);
        mScroll = mPanel.GetComponent<UIScrollView>();

        /////////////////////////////////////
        // Cache all children and place them in order
        mChildren.Clear();
        for (int i = 0; i < mTrans.childCount; ++i)
            mChildren.Add(mTrans.GetChild(i).GetComponent<mEndLessEntry>());

        mChildren.Sort(delegate(mEndLessEntry a, mEndLessEntry b) { return b.transform.localPosition.y.CompareTo(a.transform.localPosition.y); });

        /////////////////////////////////////
        mScroll.GetComponent<UIPanel>().onClipMove += OnMove;
        mScroll.restrictWithinPanel = false;
        if (mScroll.dragEffect == UIScrollView.DragEffect.MomentumAndSpring)
            mScroll.dragEffect = UIScrollView.DragEffect.Momentum;

        OnMove(mPanel);
    }



      #region View

    public void OnMove(UIPanel panel)
    {
        WrapContent();

        UpdateScrollView(panel);
    }

    public void WrapContent()
    {
        float extents = itemSize * mChildren.Count * 0.5f;
        Vector3[] corners = mPanel.worldCorners;
        
        for (int i = 0; i < 4; ++i)
        {
            Vector3 v = corners[i];
            v = mTrans.InverseTransformPoint(v);
            corners[i] = v;
        }
        Vector3 center = Vector3.Lerp(corners[0], corners[2], 0.5f);

        //Debug.Log(" center  " + center);

        float min = corners[0].y - itemSize;
        float max = corners[2].y + itemSize;

        foreach (var t in mChildren)
        {
            float distance = t.transform.localPosition.y - center.y;

            if (distance < -extents)
            {
                t.transform.localPosition += new Vector3(0f, extents * 2f, 0f);
                distance = t.transform.localPosition.y - center.y;
            }
            else if (distance > extents)
            {
                t.transform.localPosition -= new Vector3(0f, extents * 2f, 0f);
                distance = t.transform.localPosition.y - center.y;
            }
        }
    }

    public void UpdateScrollView(UIPanel panel)
    {
        float localY = panel.clipOffset.y;

        foreach (mEndLessEntry item in mChildren)
        {
            float offsetY = item.transform.localPosition.y;
            
            float factor = ((offsetY - localY) + 300) / 600;

            item.UpdateScrollView(factor);
        }
    }
    #endregion
}
